using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Base;
using Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel.Mappings;
using Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel.Mappings.Enums;
using Microsoft.Practices.RepositoryFactory.CustomPages.Helpers;

namespace Microsoft.Practices.RepositoryFactory.CustomPages.CreateRepositoryPages
{
    public partial class CreateOperationPage : UserControl, ICreateOperationView
    {
        private CreateOperationViewPresenter presenter;

        public CreateOperationPage(CreateOperationViewPresenter presenter)
        {
            InitializeComponent();
            this.presenter = presenter;
            this.presenter.View = this;
        }

        public void FillStoredProcedures(List<StoredProcedure> storedProcedures)
        {
            storedProcedureCombo.DisplayMember = "Name";
            storedProcedureCombo.DataSource = storedProcedures;
        }

        public bool IsDataValid
        {
            get 
            {
                bool isValid = presenter.IsOperationNameValid(operationNameTextbox.Text);

                if (!isValid)
                {
                    createOperationErrorProvider.SetError(operationNameTextbox, "You should enter a unique operation name");
                }
                else 
                {
                    createOperationErrorProvider.Clear();
                }

                return isValid;
            }
        }

        public void Complete()
        {
            StoredProcedure procedure = storedProcedureCombo.SelectedItem as StoredProcedure;
            string operationName = operationNameTextbox.Text;
            presenter.OnComplete(operationName, procedure);
        }

        public void FillWithExistingData(EntityStoredProcedureMapping existingMapping)
        {
            StoredProcedure procedure = UIHelper.FindStoredProcedure(storedProcedureCombo, existingMapping.Procedure);
            storedProcedureCombo.SelectedItem = procedure;

            operationNameTextbox.Text = existingMapping.OperationName;
        }

        private void OnCreateOperationPageLoad(object sender, EventArgs e)
        {
            presenter.OnViewLoad();
        }

        public void SetOperationType(OperationType operationType)
        {
               this.operationTypeValue.Text = operationType.ToString();
        }

        private void StoreProcedureComboChanged(object sender, EventArgs e)
        {
            presenter.OnProcedureChanged(storedProcedureCombo.SelectedItem as StoredProcedure);
        }

        public void DisplayOperationName(string procedureName)
        {
            List<StoredProcedure> procedureList = 
                this.storedProcedureCombo.DataSource as List<StoredProcedure>;
            
            // only default the name of the operation if user 
            // did not already enter something there.
            if (String.IsNullOrEmpty(this.operationNameTextbox.Text) ||
                procedureList.Exists(delegate(StoredProcedure procedure) 
                    {return (procedure.Name == this.operationNameTextbox.Text);})
                )
            this.operationNameTextbox.Text = procedureName;
        }

        public void DisplayEntityName(string entityName)
        {
            this.entityNameTextbox.Text = entityName;
            this.entityNameTextbox.ReadOnly = true;
            this.entityNameTextbox.Enabled = false;
        }

        private void OperationNameHelpTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
